package com.seleniti.enpareja.controller.ui.view;

import org.ioc.commons.flowcontrol.actioncontroller.ActionController;
import org.ioc.commons.flowcontrol.eventbus.EventBus;
import org.ioc.commons.flowcontrol.eventbus.EventHandler;
import org.ioc.commons.flowcontrol.operationmanager.OperationManager;
import org.ioc.commons.model.globaldata.GlobalDataController.Consumer;
import org.ioc.commons.model.globaldata.IsGlobalDataConsumer;
import org.ioc.commons.utils.FormatterLogger;

import com.seleniti.enpareja.controller.common.GlobalData;
import com.seleniti.enpareja.controller.control.place.EnParejaPlace;
import com.seleniti.enpareja.controller.ui.base.EnParejaViewBasePresenter;
import com.seleniti.enpareja.controller.ui.view.SignUpView.Action;
import com.seleniti.enpareja.controller.ui.view.SignUpView.Event;
import com.seleniti.enpareja.controller.ui.view.SignUpView.Operation;
import com.seleniti.enpareja.controller.ui.widget.UserForm;

/**
 * Presenter for the SignUp view
 * 
 * @author Jesús Lunar Pérez
 * 
 */
public class SignUpViewPresenter extends
		EnParejaViewBasePresenter<SignUpView, SignUpView.Event, SignUpView.Operation, SignUpView.Action> implements
		SignUpView.Presenter, IsGlobalDataConsumer<GlobalData> {

	private static final FormatterLogger logger = FormatterLogger.getLogger(SignUpViewPresenter.class);

	private Consumer<GlobalData> consumer;

	public SignUpViewPresenter(SignUpView display, EventBus<Event> eventbus,
			OperationManager<Operation> operationManager, ActionController<Action> actionController) {
		super(display, eventbus, operationManager, actionController);
		this.consumer = this.globalDataController.registerGlobalDataConsumer(this);
		this.consumer.requires(GlobalData.UserAgent);
	}

	@Override
	public void onInitDisplay() {
		super.onInitDisplay();
		logger.entering("onInitDisplay");

		display.userForm().caption().setText("Nuevo usuario:");
		display.userForm().saveActionText().setText("Crear");
		display.userForm().setTryLoginOnSave(true);

		logger.exiting("onInitDisplay");
	}

	protected void bindComponents() {
		super.bindComponents();
		display.userForm().eventBus().bindEvent(UserForm.Event.UserSaved, new EventHandler<UserForm.Event>() {

			@Override
			public void handleEvent(Object sender, com.seleniti.enpareja.controller.ui.widget.UserForm.Event event,
					Object... params) {
				Boolean loggedIn = (params != null && params.length > 1) ? (Boolean) params[1] : null;
				logger.info("User saved OK. Is logged in? {0}", loggedIn);
				if (Boolean.TRUE.equals(loggedIn)) {
					placeController.goTo(EnParejaPlace.Main, true);
				} else {
					String userEmail = (String) params[0];
					placeController.goTo(EnParejaPlace.LoginPlace, true, EnParejaPlace.Main, userEmail);
				}
			}
		});
	}

	@Override
	public void onActive() {
		super.onActive();

		shell.refreshButtonVisibility().setVisible(false);
		shell.gettingStartedInfoVisible().setVisible(false);
		this.consumer.ensureRequirements();
	}

	@Override
	public void onRequiredGlobalDataReady() {
		showGoBackButtonIfRequired();
	}

	@Override
	public void onRequiredGlobalDataNotReadyYet() {
		/*
		 * Nothing to do
		 */
	}

	@Override
	public void onRequiredGlobalDataUpdated(GlobalData globalDataUpdated) {
		showGoBackButtonIfRequired();
	}
}
